trigger insertOutletStock on Outlet_Definition_Product_ADL__c (before insert) {

    Outlet_Definition_Product_ADL__c outletADL = Trigger.new.get(0);
    Id outletDefId;
    if(outletADL.Outlet_definition__c != null)
    {
        outletDefId = outletADL.Outlet_definition__c;
    }
    else 
    {
        outletADL.addError('You must associate an outlet definition');
    }
    
    
    Map<Id,Account> outletsMap;
    
    if(outletDefId != null)
    {
        
        outletsMap = new Map<Id,Account>([Select Id from Account Where Outlet_Definition__c =: outletDefId]);
      
      
      if(outletADL.Product__c != null)
        {
             if(!outletsMap.isEmpty())
            {
                 for(Outlet_Stock__c existingOutletObj : [Select Id, Account__c, Product__c
                                                                    From Outlet_Stock__c 
                                                                        Where Product__c =: outletADL.Product__c 
                                                                            AND Account__c IN: outletsMap.keySet()])
                {
                    if(outletsMap.containsKey(existingOutletObj.Account__c))
                    {   
                        //if we match the existing outlet objective record
                        //against an account, we remove the key from the outletsMap
                        outletsMap.remove(existingOutletObj.Account__c);
                        
                        
                        
                        
                    }
                }
                
                  if(!outletsMap.isEmpty())
                {
                    //5. This list will contain the outlet objectives that we want to insert.
                    //   We populate this whenever we don't find an outlet objective that
                    //   matches an existing outlet that is linked to this outlet definition 
                    //   objective via the outlet definition.
                    List<Outlet_Stock__c> newStock = new List<Outlet_Stock__c>(); 
                    
                    for(Id accountId : outletsMap.keySet())
                    {
                        Outlet_Stock__c Stock = new Outlet_Stock__c();
                        
                        stock.Account__c = accountId;
                        stock.Product__c = outletADL.Product__c;
                        
                        newStock.add(Stock);
                    } 
                    
                    if(!newStock.isEmpty())
                    {
                        insert newStock;
                    } 
                 }  
            }
            
               else
            {
                outletADL.addError('No products associated with the outlet definition products ADL');
            }
        
        }  
        else
        {
            outletADL.addError('No outlet definition associated with the outlet definition product');
        }   
      
    }
    
   
    
    
}